package com.cndcare.msc.netty.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.cndcare.msc.client.WxQrCodeClient;
import com.cndcare.msc.cloud.base.result.RestResult;
import com.cndcare.msc.config.SystemConfig;
import com.cndcare.msc.netty.bean.BindAlarmBean;
import com.cndcare.msc.netty.dao.jpa.BindAlarmBeanJpa;
import com.cndcare.msc.netty.service.AlarmService;
import com.cndcare.msc.netty.vo.SearchVo;
import com.github.pagehelper.PageInfo;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/alarm")
public class BindAlarmController {

    @Autowired
    private WxQrCodeClient wxQrCodeClient;
    @Autowired
    private SystemConfig systemConfig;
    @Autowired
    private AlarmService alarmService;
    @Autowired
    private BindAlarmBeanJpa bindAlarmBeanJpa;

    @RequestMapping("/bindAlarm")
    public String bindAlarm(
            Map<String, Object> map,
            @RequestParam(value = "count", defaultValue = "0") Long count
    ) throws IOException {
        String qrSceneStr = "bind:alarm:nettyAlarm:bindAlarmMqNettyAlarm";

        RestResult<WxMpQrCodeTicket> restResult = wxQrCodeClient.qrCodeCreateTmpTicket(
                systemConfig.getAppId(), qrSceneStr, 60);
        if (restResult.getCode() != 0) {
            throw new RemoteException(restResult.getMsg());
        }

        BufferedImage bufferedImage = QrCodeUtil.generate(restResult.getData().getUrl(), 300, 300);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
        ImageIO.write(bufferedImage, "png", baos);//写入流中
        byte[] bytes = baos.toByteArray();//转换成字节]
        String imgBase64 = Base64.encode(bytes);

        map.put("imgBase64", imgBase64);
        map.put("count", count);

        return "netty/alarm/bind_alarm";
    }

    @RequestMapping("/listBindAlarm")
    public String listBindAlarm() {

        return "netty/alarm/bind_alarm_list";
    }

    @RequestMapping("/listBindAlarmByParams")
    @ResponseBody
    public PageInfo<BindAlarmBean> listBindAlarmByParams(
            @RequestParam(value = "pn", defaultValue = "1") Integer pn,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            SearchVo searchVo
    ) {
        return alarmService.listBindAlarmByParams(pn, pageSize, searchVo);
    }

    @RequestMapping("/bindConfirm")
    @ResponseBody
    public Map<String, Object> bindConfirm(
            @RequestParam("count") Long count
    ) {
        Map<String, Object> jsonMap = new HashMap<>();
        boolean flag =  alarmService.bindConfirm(count);

        jsonMap.put("flag", flag);

        return jsonMap;
    }

    @RequestMapping("/delBindAlarmBean")
    @ResponseBody
    public Map<String, Object> delBindAlarmBean(
            @RequestParam("id") Long id
    ) {
        Map<String, Object> jsonMap = new HashMap<>();
        try {
            bindAlarmBeanJpa.deleteById(id);
            jsonMap.put("flag", true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonMap.put("flag", false);
            jsonMap.put("errorMsg", e.getMessage());
        }

        return jsonMap;
    }
}
